import datetime
from stand_requests import request_station_code
from serial_operate import get_stand_sql_data, stand_frozen_data
from tools import deal_time, get_previous_15min_time, get_all_meter_addr, modify_centos_current_date_restart, \
    get_collect_err, assert_data, make_collect_err_data

com_name = 'com4'
ip = "192.168.253.113"

# 1、插入昨天、今天的采集数据,昨天误差数据
print("开始第一步骤")

# 设置站端时间为PC当前时间
modify_centos_current_date_restart(ip)
make_collect_err_data(ip, -1, 2, 0, 1)

start, end = get_previous_15min_time()
list_meter_addr = get_all_meter_addr(ip)

start_time = deal_time(start)
end_time = deal_time(end)

# 循环所以表计地址
for meter_addr in list_meter_addr:
    collect_ere_data = get_stand_sql_data(start_time, end_time, list_meter_addr[0], com_name, ip)
    print(collect_ere_data)
    recv_data = collect_ere_data.get("hex_recv")

    # 【检查点2】不检查，不比较日志中的保温
    # 【检查点1、3、4】报文格式，频率幅值相位比差角差精度和数据库对比
    # 采集数据核对
    assert recv_data[56:64] == "20680200"
    # 频率幅值相位
    assert_data(4, collect_ere_data.get("message_data")[2] / 10000, collect_ere_data.get("sql_data")[0])
    assert_data(4, collect_ere_data.get("message_data")[3] / 10000, collect_ere_data.get("sql_data")[1])
    assert_data(2, collect_ere_data.get("message_data")[4] / 100, collect_ere_data.get("sql_data")[2])

    # 计算数据核对，比差角差
    assert recv_data[66:74] == "20690200"
    assert_data(3, collect_ere_data.get("message_data")[5] / 1000, collect_ere_data.get("sql_data")[3])
    assert_data(3, collect_ere_data.get("message_data")[6] / 1000, collect_ere_data.get("sql_data")[4])
    # 数据标识为5002,包含2068和2069两个OAD, 时间为请求报文前1个小时15分的时间点
    assert recv_data[34:42] == '50020200'
    assert recv_data[56:64] == '20680200'
    assert recv_data[66:74] == '20690200'
    assert (datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") - datetime.datetime.strptime(
        collect_ere_data.get("message_data")[1], "%Y-%m-%d %H:%M:%S")).total_seconds() == 4500.0

request_station_code(ip, 8082, "oetsky", "oetsky@45296", "65000001")
list_meter_addr = get_all_meter_addr(ip)
_, collect_time, _, _, _, _, angle, _, _, _ = stand_frozen_data(start_time, end_time, list_meter_addr[0], com_name)
_, _, _, _, sql_angle_error, _ = get_collect_err(ip, list_meter_addr[0], collect_time)
# 【检查点5】地址域为修改后的65开头的互感器表记地址，角差精度为千分位
assert_data(3, angle / 1000, sql_angle_error)

request_station_code(ip, 8082, "oetsky", "oetsky@45296", "35000001")
list_meter_addr = get_all_meter_addr(ip)
_, collect_time, _, _, _, _, angle, _, _, _ = stand_frozen_data(start_time, end_time, list_meter_addr[0], com_name)
_, _, _, _, sql_angle_error, _ = get_collect_err(ip, list_meter_addr[0], collect_time)
# 【检查点6】地址域为修改后的35开头的互感器表记地址，角差精度为千分位
assert_data(3, angle / 1000, sql_angle_error)

request_station_code(ip, 8082, "oetsky", "oetsky@45296", "44000001")
list_meter_addr = get_all_meter_addr(ip)
_, collect_time, _, _, _, _, angle, _, _, _ = stand_frozen_data(start_time, end_time, list_meter_addr[0], com_name)
_, _, _, _, sql_angle_error, _ = get_collect_err(ip, list_meter_addr[0], collect_time)
# 【检查点7】地址域为修改后的44开头的互感器表记地址，角差精度为百分位
assert_data(2, angle / 100, sql_angle_error)
